package palidino76.rs2.io.packets;

import palidino76.rs2.*;
import palidino76.rs2.player.misc.*;
import palidino76.rs2.player.combat.*;
import palidino76.rs2.player.games.*;
import palidino76.rs2.player.Player;
import palidino76.rs2.util.Misc;

public class PlayerOption1 implements Packet {
    /**
     * Handles the first player option.
     * @param p The Player which the frame should be handled for.
     * @param packetId The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
	PlayerMethods pm = new PlayerMethods(p);
	try {
		if (p.inDuelArena()) {
			Player player = Server.engine.players[p.duelFriend];
			if (player == null)
				return;
			if (p.inDuelArena() && player.inDuelArena()) {
				p.getDuelClass().initializeDuelAcception();
				return;
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	/**
	 * Pvp system.
	 */
	p.enemyIndex = p.stream.readUnsignedWordBigEndian();
	Player en = Engine.players[p.enemyIndex];
	if (p == null || en == null || p.inDuelArena() || en.inDuelArena() || p.isDead || p.randomDelay > 0) {
		p.resetAttack();
		p.enemyIndex = 0;
		return;
	}
	if (p.muteExpect2) {
		en.muteType = 0;
	   	p.muteExpect2 = false;
	}
	if (p.muteExpect) {
		en.muteType = 1;
		p.muteExpect = false;
	}
	if (p.clanWarsFightArea() && en.clanWarsFightArea()) {
		if (p.blackTeam && en.blackTeam || p.whiteTeam && en.whiteTeam) {
			p.resetAttack();
			p.enemyIndex = 0;
			p.frames.sendMessage(p, "You cannot attack your own team mate!");
			return;
		}
	}
	/*if (p.inEdgePk() && en.inEdgePk() || !p.multiZone() && !en.multiZone()) {
		if (en.combatWith != p.playerId && en.combatWith != 0) {
			Server.engine.playerMovement.resetWalkingQueue(p);
			p.requestFaceTo(p.enemyIndex + 32768);
			p.requestFaceTo(65535);
			p.enemyIndex = 0;
			p.resetAttack();
			p.frames.sendMessage(p, "That player is already in combat.");
			return;
		}
	}*/
	if (p.duelFight() && en.duelFight()) {
		if (en.duelFriend == p.playerId) {
		    if (!p.castAuto) {
			if (p.countType > -1) {
				p.resetAttack();
				return;
			}
				pm.setAttackPlayer(true);
		    } else {
				Server.engine.playerMovement.resetWalkingQueue(p);
				p.usingAutoCast = true;
		    }
		} else {
			p.frames.sendMessage(p, "You cannot attack this player!");
			p.resetAttack();
			return;
		}
	}
	if (!Server.engine.wildernessArea(en.absX, en.absY) && !en.duelFight() && !p.AtPits()) {
		p.frames.sendMessage(p, "You cannot attack other players in this area.");
		p.resetAttack();
		return;
	}
	if (Server.engine.wildernessArea(en.absX, en.absY) && p.BHOutside() || p.BHOutside2()) {
		p.frames.sendMessage(p, "You cannot attack players outside the BountyHunter craters.");
		p.resetAttack();
		return;
	}
	if (Server.engine.wildernessArea(en.absX, en.absY) && p.inClan() || en.inClan()) {
		p.frames.sendMessage(p, "You cannot attack players in your clan!");
		p.resetAttack();
		return;
	}
	if (p.equipment[3] == 4734) {
	    if (p.equipment[13] != 4740) {
		p.frames.sendMessage(p, "You cannot use this ammo with your bow.");
		Server.engine.playerMovement.resetWalkingQueue(p);
		p.resetAttack();
		p.requestFaceTo(p.enemyIndex + 32768);
		return;
	} else {
		pm.setAttackPlayer(true);
	    }
	}
	if (p.equipment[3] == 9185) {
	    if (!p.hasBolts()) {
		p.frames.sendMessage(p, "You cannot use this ammo with your bow.");
		Server.engine.playerMovement.resetWalkingQueue(p);
		p.resetAttack();
		p.requestFaceTo(p.enemyIndex + 32768);
		return;
	    }
	}
	if (p.equipment[3] == 861) {
	    if (!p.hasArrows()) {
		p.frames.sendMessage(p, "You cannot use this ammo with your bow.");
		Server.engine.playerMovement.resetWalkingQueue(p);
		p.resetAttack();
		p.requestFaceTo(p.enemyIndex + 32768);
		return;
	    }
	}
	if (p.equipment[3] == 11235) {
	    if (!p.hasArrowsDB()) {
		p.frames.sendMessage(p, "You cannot use this ammo with your bow.");
		Server.engine.playerMovement.resetWalkingQueue(p);
		p.resetAttack();
		p.requestFaceTo(p.enemyIndex + 32768);
		return;
	    }
	}
	if (p.equipment[3] == 4214 && p.equipment[13] >= 0) {
		p.frames.sendMessage(p, "You cannot use this ammo with your bow.");
		Server.engine.playerMovement.resetWalkingQueue(p);
		p.resetAttack();
		p.requestFaceTo(p.enemyIndex + 32768);
		return;
	}
	if (!Server.engine.wildernessArea(p.absX, p.absY) && !p.duelFight() && !p.AtPits()  && p.BHOutside() || p.BHOutside2() || p.inClan() || en.inClan()) {
		p.frames.sendMessage(p, "You cannot attack other players in this area.");
		p.resetAttack();
		return;
	}
	if (!p.castAuto) {
		if (p.inEdgePk() && en.inEdgePk()) {
			p.battleCDelay = 20;
			if (p.battleCount <= 0)
				p.battleCount++;
		}
		pm.setAttackPlayer(true);
	} else {
		p.requestFaceTo(p.enemyIndex + 32768);
		Server.engine.playerMovement.resetWalkingQueue(p);
		p.usingAutoCast = true;
	}
    }
}
